package top.bingoz.wanandroid.data.http.transformer

import com.trello.rxlifecycle2.LifecycleTransformer
import io.reactivex.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.reactivestreams.Publisher
import top.bingoz.wanandroid.common.utils.DeviceUtils

/**
 * @author su
 * @date 2018/7/29
 * @description
 */
class SimpleTransformer<T>() : FlowableTransformer<T, T>, ObservableTransformer<T, T> {

    private var lifecycle: LifecycleTransformer<T>? = null

    constructor(lifecycle: LifecycleTransformer<T>?) : this() {
        this.lifecycle = lifecycle
    }

    override fun apply(upstream: Flowable<T>): Publisher<T> {
        val observable = upstream.doOnSubscribe {
            if (DeviceUtils.isWifiProxy()) {
                it.cancel()
            }
        }.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())

        if (lifecycle != null) {
            return observable.compose(lifecycle)
        }
        return observable

    }

    override fun apply(upstream: Observable<T>): ObservableSource<T> {
        val observable = upstream.doOnSubscribe {
            if (DeviceUtils.isWifiProxy()) {
                it.dispose()
            }
        }.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())

        if (lifecycle != null) {
            return observable.compose(lifecycle)
        }
        return observable

    }
}